<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>文件上传测试</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            max-width: 600px;
            margin: 50px auto;
            padding: 20px;
        }
        h1 {
            color: #333;
        }
        input[type="file"] {
            margin: 20px 0;
        }
        button {
            padding: 10px 20px;
            background: #007bff;
            color: white;
            border: none;
            cursor: pointer;
            font-size: 16px;
        }
        button:hover {
            background: #0056b3;
        }
        button:disabled {
            background: #ccc;
            cursor: not-allowed;
            opacity: 0.6;
        }
        #result {
            margin-top: 20px;
            padding: 15px;
            border-radius: 4px;
        }
        .success {
            background: #d4edda;
            color: #155724;
        }
        .error {
            background: #f8d7da;
            color: #721c24;
        }
    </style>
</head>
<body>
    <h1>文件上传测试</h1>
    <input type="file" id="fileInput">
    <br>
    <button id="uploadBtn" onclick="uploadFile()">上传</button>
    <div id="result"></div>

    <script>
        async function uploadFile() {
            const fileInput = document.getElementById('fileInput');
            const result = document.getElementById('result');
            const uploadBtn = document.getElementById('uploadBtn');

            if (!fileInput.files[0]) {
                result.textContent = '请选择文件';
                result.className = 'error';
                return;
            }

            const file = fileInput.files[0];
            result.textContent = `正在上传: ${file.name} (${file.size} bytes)...`;
            result.className = '';

            // 禁用按钮
            uploadBtn.disabled = true;

            try {
                const response = await fetch('/ApiServer/Upload', {
                    method: 'POST',
                    body: file
                });

                const data = await response.json();

                if (response.status === 413) {
                    result.textContent = `状态: ${response.status} - 文件过大 (${file.size} bytes > 50KB)`;
                    result.className = 'error';
                } else if (response.ok) {
                    result.textContent = `状态: ${response.status} - 上传成功`;
                    result.className = 'success';
                } else {
                    result.textContent = `状态: ${response.status} - ${data.message || '上传失败'}`;
                    result.className = 'error';
                }
            } catch (error) {
                result.textContent = `错误: ${error.message}`;
                result.className = 'error';
            } finally {
                // 恢复按钮
                uploadBtn.disabled = false;
            }
        }
    </script>
</body>
</html>
